﻿using System;
using IService.Model;
using IService.Business;
using IService.DataDLL.Utility;
using IService.Utility;

namespace IService.Admin.Pages
{
	public partial class EditArticle : System.Web.UI.Page
	{
		private News _info;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!base.IsPostBack)
			{
				CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
				_FileBrowser.BasePath = "/ckfinder/";
				_FileBrowser.SetupCKEditor(this.newsContentCKeditor);
				InitPageData();
			}
		}

		protected void CheckAction(string action)
		{
			if (!ParamHandler.IsLegalForString(action) || (action != "add" && action != "edit"))
			{
				Response.Redirect(StringUtil.GetAppConfigString("AdminParamErrorPage"));
			}
		}
		protected void CheckIntType(string param)
		{
			if (!ParamHandler.IsLegalForInt(param))
			{
				Response.Redirect(StringUtil.GetAppConfigString("AdminParamErrorPage"));
			}
		}

		protected void btnBack_Click(object sender, EventArgs e)
		{
			base.Response.Redirect("~/Admin/Pages/ListNews.aspx?$_k=" + new Random().NextDouble());
		}

		/// <summary>
		/// 初始化页面数据
		/// </summary>
		private void InitPageData()
		{
			CheckAction(Action);
			if (Action == "edit")
			{
				CheckIntType(NewsID);
				News news = CurrentNews;
				this.newsContentCKeditor.Text = news.NewsContent;
				this.HidTypeShow.Value = news.NewsTypeId.ToString();
				this.NewsTitle.Text = news.NewsTitle;
				this.CreatePeople.Text = news.PublishMan;
				this.ChkShowWebSite.Checked = news.ShowWebsite;
                //this.ArticlePic.Value = news.ArticlePic;
                //this.previewPic.ImageUrl = news.ArticlePic;
                //if (string.IsNullOrEmpty(news.ArticlePic))
                //{
                //    this.previewPic.Style.Add("display", "none");
                //}
			}
			else if (Action == "add")
			{
				this.ChkShowWebSite.Checked = false;
				this.CreatePeople.Text = GetUserName;//从session读数据
				//this.previewPic.Style.Add("display", "none");
			}
		}

		protected void BtnEditArticle_Click(object sender, EventArgs e)
		{
			string newsTitle = this.NewsTitle.Text.Replace(" ", "");
			string createPeople = this.CreatePeople.Text.Replace(" ", "");
			
			string NewsType = this.HidTypeShow.Value;
			if (string.IsNullOrEmpty(newsTitle))
			{
				this.LblError.Text = "<font color='red'>新闻标题不能为空！</font>";
			}
			else if (string.IsNullOrEmpty(NewsType))
			{
				this.LblError.Text = "<font color='red'>请选择新闻类型！</font>";
			}
			else if (string.IsNullOrEmpty(createPeople))
			{
				this.LblError.Text = "<font color='red'>发布人不能为空！</font>";
			}
            //else if (string.IsNullOrEmpty(ArticlePic))
            //{
            //    this.LblError.Text = "<font color='red'>请上传新闻主题图片！</font>";
            //}
			else
			{
				News news = CurrentNews;
				news.NewsContent = this.newsContentCKeditor.Text;
				news.PublishMan = this.CreatePeople.Text;
				news.NewsTypeId = int.Parse(NewsType);
				news.NewsTitle = this.NewsTitle.Text;
				news.ArticlePic = "";
				news.ShowWebsite = this.ChkShowWebSite.Checked;
				if (Action == "edit")
				{
					news.ID = int.Parse(NewsID);
					news.ModifyTime = DateTime.Now;
				}
				else if (Action == "add")
				{
					news.CreateTime = DateTime.Now;
				}
				bool hassuccess = news.Save();
				if (hassuccess)
				{
                    JsUtil.AlertConfirm("新闻内容保存成功！", this.Page);
                    JsUtil.RefreshParent("ListNews.aspx", this.Page);
				}
				else
				{
                    JsUtil.Alert("新闻内容保存失败！", this.Page);
				}
			}
		}

		protected string GetUserName
		{
			get
			{
				string username = string.Empty;
				DTVAdmin admin = CurrentUser;
				if (admin == null)
				{
					admin = Session["AdminSession"] as DTVAdmin;
					username = admin.DisplayName;
					CurrentUser = admin;
				}
				return username;
			}
		}

		protected string NewsID
		{
			get
			{
				return base.Request["newsId"];
			}
		}

		protected string Action
		{
			get
			{
				return base.Request["action"];
			}
		}

		private DTVAdmin CurrentUser
		{
			get
			{
				return (this.ViewState["CurrentUser"] as DTVAdmin);
			}
			set
			{
				this.ViewState["CurrentUser"] = value;
			}
		}

		protected News CurrentNews
		{
			get
			{
				News info = null;
				if (this._info != null)
				{
					info = this._info;
				}
				if (info == null)
				{
					if (ParamHandler.IsLegalForInt(NewsID))
					{
						info = DTVBusiness.NewsManager.GetNewsById(int.Parse(NewsID));
					}
					else
					{
						info = new News();
					}
					this._info = info;
				}
				return info;
			}
		}
	}
}